# -*- coding: utf-8 -*-
# @Time    : 2021/10/14 15:19
# @Author  : lpd
# @File    : Eval.py
import torch
import os
import json
import torch

import numpy as np


'''
综合评估 全连接和卷积 模型 准确率、精确率、召回率、F1值
'''
model_name = 'conv'  # 选择 fcn 或者 conv

acc_num = 0
t00, f01, f02, f03, f04, f10, t11, f12, f13, f14, f20, f21, t22, f23, f24 = 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0
f30, f31, f32, t33, f34, f40, f41, f42, f43, t44 = 0, 0, 0, 0, 0, 0, 0, 0,0,0
N=len(datalist)

for i in datalist:
    UseData = i[:-1]
    true_result = i[-1]
    flaotlist = list(map(float, UseData))
    mat = np.array(flaotlist)
    # mat = (mat-np.mean(mat))/np.std(mat)
    if model_name == 'fcn':
        test = torch.from_numpy(mat).type(torch.FloatTensor)
    if model_name == 'conv':
        mat = np.reshape(mat, (mat.shape[0], 1))
        mat = mat[np.newaxis, :]  # 手动给数据升维度
        test = torch.from_numpy(mat).type(torch.FloatTensor)
    result = net(test)
    predict_y = torch.argmax(result).item() + 1  # 由0-4 到 1-5
    print('预测值：', predict_y, '   真实值：', true_result)
    p = int(float(predict_y))-1 # 由 1-5到0-4
    t = int(float(true_result))-1
    if t == 0 and p == 0:
        t00 += 1
    elif t == 0 and p == 1:
        f01 += 1
    elif t == 0 and p == 2:
        f02 += 1
    elif t == 0 and p == 3:
        f03 += 1
    elif t == 0 and p == 4:
        f04 += 1

    elif t == 1 and p == 0:
        f10 += 1
    elif t == 1 and p == 1:
        t11 += 1
    elif t == 1 and p == 2:
        f12 += 1
    elif t == 1 and p == 3:
        f13 += 1
    elif t == 1 and p == 4:
        f14 += 1

    elif t == 2 and p == 0:
        f20 += 1
    elif t == 2 and p == 1:
        f21 += 1
    elif t == 2 and p == 2:
        t22 += 1
    elif t == 2 and p == 3:
        f23 += 1
    elif t == 2 and p == 4:
        f24 += 1

    elif t == 3 and p == 0:
        f30 += 1
    elif t == 3 and p == 1:
        f31 += 1
    elif t == 3 and p == 2:
        f32 += 1
    elif t == 3 and p == 3:
        t33 += 1
    elif t == 3 and p == 4:
        f34 += 1

    elif t == 3 and p == 0:
        f30 += 1
    elif t == 3 and p == 1:
        f31 += 1
    elif t == 3 and p == 2:
        f32 += 1
    elif t == 3 and p == 3:
        t33 += 1
    elif t == 3 and p == 4:
        f34 += 1

    elif t == 4 and p == 0:
        f40 += 1
    elif t == 4 and p == 1:
        f41 += 1
    elif t == 4 and p == 2:
        f42 += 1
    elif t == 4 and p == 3:
        f43 += 1
    elif t == 4 and p == 4:
        t44 += 1

acc = (t00+t11+t22+t33+t44)/N  # 准确率
p0 = t00/(t00+f10+f20+f30+f40)
p1 = t11/(f01+t11+f21+f31+f41)
p2 = t22/(f02+f12+t22+f32+f42)
p3 = t33/(f03+f13+f23+t33+f43)
p4 = t44/(f04+f14+f24+f34+t44)
precision = (p0+p1+p2+p3+p4)/5  # 精确率

r0 = t00/(t00+f01+f02+f03+f04)
r1 = t11/(f10+t11+f12+f13+f14)
r2 = t22/(f20+f21+t22+f23+f24)
r3 = t33/(f30+f31+f32+t33+f34)
r4 = t44/(f40+f41+f42+f43+t44)
recall = (r0+r1+r2+r3+r4)/5  # 召回率

F1 = 2*precision*recall/(precision+recall)

print("{} 模型\n准确率为：{}\n精确率为：{}\n召回率为：{}\nF1值为:{}\n"
      .format(model_name,acc,precision,recall,F1))
